package cn.yann.demo.vertx.tcp;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.net.NetSocket;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @author Yann
 */
@Slf4j
public class T1MsgHandler implements Handler<NetSocket> {

    private final Vertx vertx;

    public T1MsgHandler(Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public void handle(NetSocket event) {
        event.handler(buff -> {
            log.debug("接收消息字节长度: {}", buff.length());
            String str = new String(buff.getBytes(), StandardCharsets.UTF_8);
            EventBus eventBus = vertx.eventBus();
            log.debug("bus实例: {}", eventBus);
            eventBus.publish("test-1", "publish:" + str);
            eventBus.send("test-1", "send:" + str);
            log.debug("接收数据: {}", str);
        });
    }
}
